/*
 * tcp_client.c
 * 
 */
/*
 * -------------------------------------------------------------------
 * 
 * 头文件
 *-------------------------------------------------------------------
 */
#include <netdb.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h> //包含宏STDOUT_FILENO
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <arpa/inet.h>//包含函数inet_pton
#include <pthread.h>
/*
 * -------------------------------------------------------------------
 * 
 * 宏定义
 *-------------------------------------------------------------------
 */
#define NAME_SIZE 20
#define CONT_SIZE 128
#define LINE1 printf("\033[1;0H\033[K\033[1;0H")
#define LINE5 printf("\033[5;0H\033[K\033[5;0H")
/*
 * -------------------------------------------------------------------
 * 
 * 结构体
 *-------------------------------------------------------------------
 */
enum Chat_Opt{
	ADD,
	DEL,
	CHAT,
	ONLINE
};

typedef struct Chat_pack{
//head
	char head[5];
//data
	enum Chat_Opt chat_opt;
	char sender[NAME_SIZE];
	char recv[NAME_SIZE];
	int checknum;
	char content[CONT_SIZE];
}chat_t;
/*
 * -------------------------------------------------------------------
 * 
 * 线程函数
 *-------------------------------------------------------------------
 */
void* th_fn(void* arg)
{
	int sockfd = (int)arg;
	char buffer[512];
	while(1){
		memset(buffer,0,sizeof(buffer));
		if(read(sockfd,buffer,512)>0){
			chat_t chat;
			memset(&chat,0,sizeof(chat_t));
			memcpy(&chat,buffer,sizeof(chat_t));
			if(!strcmp(chat.head,"erro")){printf("对方不在线！\n");continue;}
			if(!strcmp(chat.head,"chat")){
				printf("\033[s");
				LINE1;
				printf("from %s : %s",chat.sender,chat.content);
				printf("\033[u");
				fflush(stdout);
			}
		}
	}
}

/*
 * -------------------------------------------------------------------
 * 
 * 主函数
 *-------------------------------------------------------------------
 */
int main(int argc, char **argv)
{
	if(argc<5){
		printf("usage: %s ip port sender recv \n",argv[0]);
		exit(1);
	}
	system("clear");
	/*步骤1：创建socket*/
	int sockfd = socket(AF_INET, SOCK_STREAM,0);
	if(sockfd<0){printf("socket error");exit(1);}
	
	//往serveraddr中填入ip,port和地址族类型
	struct sockaddr_in serveraddr;
	memset(&serveraddr,0,sizeof(serveraddr));
	serveraddr.sin_family = AF_INET;
	serveraddr.sin_port = htons(atoi(argv[2]));
	//将ip地址转换成网络字节序
	inet_pton(AF_INET,argv[1],
					&serveraddr.sin_addr.s_addr);
	/*步骤2：客户端调用connect函数连接到服务器端*/
	if(connect(sockfd, (struct sockaddr*)&serveraddr,
									sizeof(serveraddr))<0){
			perror("connect error");
			exit(1);
		}
	
	/*步骤3：调用IO函数(read/write)和服务器端进行双向通信*/
	pthread_t th;
	pthread_create(&th,0,th_fn,(void*)sockfd);
	pthread_detach(th);
	
	while(1){
		
		chat_t chat={"chat",CHAT,{'\0'},{'\0'},1,{'\0'}};
		strcpy(chat.sender,argv[3]);
		strcpy(chat.recv,argv[4]);
		printf("\033[4;0H-----------------------------------");
		LINE5;fflush(stdout);
		fgets(chat.content,CONT_SIZE,stdin);
		char buffer[512] ;
		memset(buffer,0,sizeof(buffer));
		memcpy(buffer,&chat,sizeof(chat_t));
		write(sockfd,buffer,512);
	}
	/*步骤4：关闭socket*/
	close(sockfd);
	
	
	
	return 0;
}

